//
//  Game.m
//  SWEngiOS
//
//  Created by Eric Augustine on 3/27/12.
//  Copyright 2012 UW - Parkside. All rights reserved.
//

#import "Game.h"
#include <stdlib.h>
#import "Question.h"
#import "Player.h"
#import "Bet.h"
#import "Guess.h"


@implementation Game

@synthesize name;
@synthesize players;
@synthesize numPlayers;
@synthesize scoreType;
@synthesize currentQuestion;
@synthesize nextQuestion;
@synthesize guesses;
@synthesize bets;
@synthesize questions;
@synthesize running;

//Need Constructors
-(id) init{
	if(self = [super init]){ 
		self.name=@"game";
		self.numPlayers=4;
		self.guesses = [[NSMutableArray alloc] init];
		self.bets = [[NSMutableArray alloc] init];
		self.questions = [[NSMutableArray alloc] init];
		self.players = [[NSMutableArray alloc] init];
	} 
	return self; 
}

-(id) initWithName: (NSString*)gameName andNumOfPlayers: (NSInteger)numOfPlayers{
	self.name = gameName;
	self.numPlayers = numOfPlayers;
	self.guesses = [[NSMutableArray alloc] init];
	self.bets = [[NSMutableArray alloc] init];
	self.questions = [[NSMutableArray alloc] init];
	self.players = [[NSMutableArray alloc] init];
	//load in questions from file potentially here
	
	return self;
}

-(BOOL) addPlayer: (Player*)p{
	if(numPlayers > (NSInteger)[players count]){
		[players addObject:p];
		return true;
	}else{
		return false;
	}
}

-(void) displayQuestion{
	currentQuestion = [questions objectAtIndex:nextQuestion];
	NSLog(@"%@", currentQuestion.text);
}

-(void) displayAnswer{
	NSLog(@"%@", currentQuestion.answer);
	nextQuestion++;
	currentQuestion = [questions objectAtIndex:nextQuestion];
}

-(void) receiveGuess: (Guess*)g{
	[guesses addObject:g];
}

-(void) clearGuesses{
	[guesses removeAllObjects];
}

-(void) clearBets{
	[bets removeAllObjects];
}

-(void) receiveBets: (Bet*)b{
	[bets addObject:b];
}

-(void) printBets{
	for(int i = 0; i < (int)[bets count]; i++){
		Bet* b = [bets objectAtIndex:i]; 
		NSLog(@"%@ %d %d", b.playerName, b.choice, b.chipAmt);
	}
}

-(void) winnings{
	NSInteger ans = currentQuestion.answer;
	Guess* winner = [guesses objectAtIndex:0];
	NSInteger winLocation = 0;
	for(int i = 1; i < self.guesses.count; i++ ){
		Guess* g = [guesses objectAtIndex:i];
		NSInteger diff = abs(ans - g.guess);
		if(diff < abs(ans - winner.guess)){
			winner = g;
			winLocation = i;
		}
	}
	for(int i =0; i < self.bets.count; i++){
		Bet* b = [bets objectAtIndex:i];
		if(b.choice == winLocation){
			for(int j = 0; j < (int)[players count]; j++){
				Player* p = [players objectAtIndex:j];
				if([b.playerName isEqualToString:p.playerName]){
					p.gameChips = b.chipAmt + p.gameChips;
				}
			}
		}else{
			for(int j = 0; j < self.players.count; j++){
				Player* p = [players objectAtIndex:j];
				if([b.playerName isEqualToString:p.playerName]){
					p.gameChips = p.gameChips - b.chipAmt;
					if(p.gameChips < 0){
						p.gameChips = 0;
					}
				}
			}
		}
	}	
}

-(void) resetChipBets{
	for(int i = 0; i < (int)[players count]; i++){
		Player* p = [players objectAtIndex:i];
		p.chipsBet = 0;
	}
}

/*-(NSMutableArray*) getQuestionsFromFile: (NSString*)fn{
	
}*/

@end
